{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "from pathlib import Path\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from ray import tune\n",
    "from tabulate import tabulate\n",
    "\n",
    "\n",
    "def get_noise_scores(df):\n",
    "    noise_scores = []\n",
    "    for _, row in df.iterrows():\n",
    "        logdir = row[\"logdir\"]\n",
    "        with open(Path(logdir) / \"result.json\", \"r\") as fin:\n",
    "            result = json.loads(fin.readlines()[-1])\n",
    "        noise_scores.append(result[\"noise_score\"])\n",
    "    return noise_scores\n",
    "\n",
    "\n",
    "RAY_RESULTS = Path(os.path.expanduser(\"~/ray_results\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = [\"description\", \"mean_accuracy\", \"mean_noise_score\", \"samples\"]\n",
    "table = []\n",
    "\n",
    "for exp_name, finished_iter in [(\"run_lenet_mnist\", 15),\n",
    "                                (\"run_lenet_staticstructure_kwinners_mnist\", 15),\n",
    "                                (\"run_lenet_staticstructure_mnist\", 15),\n",
    "                                (\"run_lenet_backpropstructure_mnist\", 50)]:\n",
    "    analysis = tune.Analysis(RAY_RESULTS / exp_name)\n",
    "    df = analysis.dataframe()\n",
    "    df = df.loc[df[\"training_iteration\"] == finished_iter]\n",
    "\n",
    "    table.append([exp_name, \n",
    "                  np.mean(df[\"mean_accuracy\"]),\n",
    "                  np.mean(get_noise_scores(df)),\n",
    "                  df.shape[0]])\n",
    "    \n",
    "print(tabulate(table, headers, tablefmt=\"orgtbl\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
